//
//  EnterPriseCustomerRegistVC.m
//  ForestPack
//
//  Created by jimmy on 2018/7/27.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "EnterPriseCustomerRegistVC.h"
#import "FPEnterpriseRegisViewController.h"
#import "FPEnterpriseRegisViewController.h"
#import "UIView+Utils.h"

@interface EnterPriseCustomerRegistVC ()<UITextFieldDelegate>

@property (nonatomic,strong)  UITextField       * phoneNumRegis;
@property (nonatomic,strong)  UITextField       * verticodeNum;
@property (nonatomic,strong)  OperateButton     * nextButton;
@property (nonatomic,strong)  UITextField       * passwordNum ;

@property (nonatomic,strong) NSTimer            * verCodeTimer;//验证码计时器
@property (nonatomic,assign) NSInteger            verCodeTime;
@property (nonatomic,strong) UIButton           * verCodeBtn;//发送验证码按钮

@end

@implementation EnterPriseCustomerRegistVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = YES;
    
}


- (void)viewDidLoad
{
    [super viewDidLoad];

    [self setTitle:@"企业用户注册"];
    [self setViewUI];

}

-(void)backClick
{
    [self.navigationController popViewControllerAnimated:true];
}

-(void)nextClick
{
    if (self.phoneNumRegis.text.length <= 0 || self.verticodeNum.text.length <= 0 || self.passwordNum.text.length <= 0) {
        [HttpManager showNoteMsg:@"信息请填写完整"];
        return;
    }
    if (![FPLoginAndRegisterTools checkYzmField:self.verticodeNum.text]) {
        [HttpManager showNoteMsg:@"验证码有误，请重新输入"];
    }
    [self getVerifyCode];
    FPEnterpriseRegisViewController *vc = [[FPEnterpriseRegisViewController alloc] init];
    vc.phoneNumRegis = self.phoneNumRegis.text;
    vc.passwordNum = self.passwordNum.text;
    [self.navigationController pushViewController:vc animated:true];
}

- (void)getVerifyCode
{
    NSString *url = @"";
    NSDictionary *parameter = @{};
    if ([self.phoneNumRegis.text isEqualToString:@""] || self.phoneNumRegis.text.length != 11) {
        [HttpManager showNoteMsg:@"手机号格式错误"];
        return;
    }
    url = [NSString stringWithFormat:@"%@app/mobile",REQUESTHEADER];//先验证一下手机号是否可以注册，可以的话，再能发送验证码
    parameter = @{@"mobile":self.phoneNumRegis.text};
    [HttpManager postHttpRequestByPost:url andParameter:parameter success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
        if ([status isEqualToString:@"1"]) {
            [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@app/mobile_send_code",REQUESTHEADER] andParameter:@{@"mobile":self.phoneNumRegis.text,@"type":@"1"} success:^(id successResponse) {
                NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
                if ([status isEqualToString:@"1"]) {
//                    [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
                    
                    
                    self.verCodeTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerCountAction) userInfo:nil repeats:YES];
                    self.verCodeTime = 60;
                    [self.verCodeTimer fire];
                    self.verCodeBtn.backgroundColor = UIColorFromRGB(@"#d1d0d0", 1);
                    self.verCodeBtn.enabled = NO;
                }else {
                    [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
                }
            } andFailure:^(id failureResponse) {
                [HttpManager showFail];
            }];
            
            
        }else{
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
    
}

- (void)timerCountAction{
    self.verCodeTime--;
    
    self.verCodeBtn.enabled = NO;
    [self.verCodeBtn setTitle:[NSString stringWithFormat:@"%ld秒后重发",(long)self.verCodeTime] forState:UIControlStateNormal];
    if (self.verCodeTime == 0) {
        self.verCodeTime = 60;
        self.verCodeBtn.backgroundColor = ThemeColor;
        self.verCodeBtn.enabled = YES;
        [self.verCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.verCodeTimer invalidate];
        self.verCodeTimer = nil;
    }
}

-(void)setViewUI
{
    
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(13, APP_STATUSBAR_HEIGHT, 22, 22)];
    [leftButton setImage:[UIImage imageNamed:@"返回红色"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftButton];
    
    UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, SCREEN_WIDTH, 20)];
    titleL.text = @"企业用户注册";
    titleL.textAlignment = NSTextAlignmentCenter;
    titleL.font = Kfont(18);
    titleL.textColor = FONTCOLOR_BLACK;
    [self.view addSubview:titleL];
    
    UIView *bjView = [[UIView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT + 60, SCREEN_WIDTH, 178)];
    [self.view addSubview:bjView];
    
    UIImageView *nameIamge = [[UIImageView alloc] initWithFrame:CGRectMake(40, 31, 14, 23)];
    nameIamge.image = [UIImage imageNamed:@"手机号"];
    [bjView addSubview:nameIamge];
    
    self.phoneNumRegis = [[UITextField alloc] initWithFrame:CGRectMake(nameIamge.right+10, 31, SCREEN_WIDTH - 104, 20)];
    self.phoneNumRegis.placeholder = @"请输入您注册的手机号";
    self.phoneNumRegis.delegate = self;
    self.phoneNumRegis.secureTextEntry = NO;
    self.phoneNumRegis.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.phoneNumRegis.textColor = UIColorFromRGB(@"504848", 1);
    self.phoneNumRegis.keyboardType = UIKeyboardTypePhonePad;
    self.phoneNumRegis.tag = 200;
    [bjView addSubview:self.phoneNumRegis];
    
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(40, self.phoneNumRegis.bottom + 15, SCREEN_WIDTH - 80, 1)];
    line1.backgroundColor = RGBACOLOR(238, 238, 238, 1);
    [bjView addSubview:line1];
    
    UIImageView *codeIamge = [[UIImageView alloc] initWithFrame:CGRectMake(40, line1.bottom + 31, 14, 23)];
    codeIamge.image = [UIImage imageNamed:@"验证码"];
    [bjView addSubview:codeIamge];
    
    self.verticodeNum = [[UITextField alloc] initWithFrame:CGRectMake(codeIamge.right+10, line1.bottom + 31, SCREEN_WIDTH - 190, 20)];
    self.verticodeNum.delegate = self;
    self.verticodeNum.placeholder = @"请输入您的验证码";
    self.verticodeNum.secureTextEntry = NO;
    self.verticodeNum.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.verticodeNum.keyboardType = UIKeyboardTypePhonePad;
    self.verticodeNum.textColor = UIColorFromRGB(@"504848", 1);
    self.verticodeNum.tag = 201;
    [bjView addSubview:self.verticodeNum];
    
    self.verCodeBtn = [[UIButton alloc] init];
    self.verCodeBtn.frame = CGRectMake(SCREEN_WIDTH - 40 - 84, line1.bottom + 30, 84, 26);
    self.verCodeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    self.verCodeBtn.layer.cornerRadius = 13;
    self.verCodeBtn.layer.masksToBounds = YES;
    self.verCodeBtn.backgroundColor = ThemeColor;
    [self.verCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.verCodeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.verCodeBtn addTarget:self action:@selector(getVerifyCode) forControlEvents:UIControlEventTouchUpInside];
    [bjView addSubview:self.verCodeBtn];
    
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(40, self.verticodeNum.bottom + 15, SCREEN_WIDTH - 80, 1)];
    line2.backgroundColor = RGBACOLOR(238, 238, 238, 1);
    [bjView addSubview:line2];
    
    UIImageView *passwordIamge = [[UIImageView alloc] initWithFrame:CGRectMake(40, line2.bottom + 31, 14, 23)];
    passwordIamge.image = [UIImage imageNamed:@"密码"];
    [bjView addSubview:passwordIamge];
    
    self.passwordNum = [[UITextField alloc] initWithFrame:CGRectMake(passwordIamge.right+10, line2.bottom + 31, SCREEN_WIDTH - 104, 20)];
    self.passwordNum.delegate = self;
    self.passwordNum.placeholder = @"请输入您的登录密码";
    self.passwordNum.secureTextEntry = YES;
    self.passwordNum.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.passwordNum.textColor = UIColorFromRGB(@"504848", 1);
    self.passwordNum.tag = 102;
    [bjView addSubview:self.passwordNum];
    
    UIView *line3 = [[UIView alloc] initWithFrame:CGRectMake(40, self.passwordNum.bottom + 15, SCREEN_WIDTH - 80, 1)];
    line3.backgroundColor = RGBACOLOR(238, 238, 238, 1);
    [bjView addSubview:line3];
    
    self.nextButton = [[OperateButton alloc] initWithFrame:CGRectMake(13, SCREEN_HEIGHT - 164, SCREEN_WIDTH - 26, 44)];
    [self.nextButton setTitle:@"下一步" forState:UIControlStateNormal];
    [self.nextButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.nextButton addTarget:self action:@selector(nextClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.nextButton];
    
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self.nextButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:true];
}



@end
